package com.hyt.it.ogt.ykcj.service.exchange.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hyt.it.ogt.ykcj.common.enums.ExamFormEnums;
import com.hyt.it.ogt.ykcj.common.exception.CustomException;
import com.hyt.it.ogt.ykcj.domain.exam.ExamInfo;
import com.hyt.it.ogt.ykcj.domain.vo.ExamInfoVo;
import com.hyt.it.ogt.ykcj.feign.kw.AreaClient;
import com.hyt.it.ogt.ykcj.feign.model.ResultListEntity;
import com.hyt.it.ogt.ykcj.feign.model.TaskCandidateAreaVO;
import com.hyt.it.ogt.ykcj.mapper.exam.ExamInfoMapper;
import com.hyt.it.ogt.ykcj.mapper.grade.EnterpriseExamineeInfoMapper;
import com.hyt.it.ogt.ykcj.service.exchange.IAreaService;

/**
 * description
 * 远程获取考试任务下的区域服务
 *
 * @author yaojian
 * @createTime 2022/01/19
 */
@Service
public class AreaServiceImpl implements IAreaService {

    @Autowired
    private AreaClient areaClient;
    @Autowired
    private ExamInfoMapper examInfoMapper;

    @Autowired
    private EnterpriseExamineeInfoMapper enterpriseExamineeInfoMapper;

    @Override
    public List<TaskCandidateAreaVO> getCandidateProvinceByExamId(String examId) {

        ExamInfo examInfo = getExamInfo(examId);
        //考试组
        Set<String> taskIds = new HashSet<>();
        Set<String> examIds = new HashSet<>();
        if(ExamFormEnums.FORM_EXAM_GROUP.getCode().equals(examInfo.getExamForm())){
             //关联考试
            List<ExamInfoVo> examInfoVos = null;
            if(null != examInfo  ){
                Map<String,Object> parms = new HashMap<>();
                parms.put("relationId",examId);
                examInfoVos = examInfoMapper.selectEnterpriseExamInfoByParms(parms);
            }

            //合并考试
            if(CollectionUtils.isNotEmpty(examInfoVos) && examInfoVos.size()>1){
                taskIds = examInfoVos.stream().filter(e->StringUtils.isNotEmpty(e.getTaskId())).map(e->e.getTaskId()).collect(Collectors.toSet());
                examIds = examInfoVos.stream().map(e->e.getId()).collect(Collectors.toSet());
            }/*else {
                //合并科目
                taskIds.add(examInfo.getTaskId());
                examIds.add(examInfo.getId());
             }*/
            if (CollectionUtils.isEmpty(taskIds)){
                return Collections.emptyList();
            }
            List<TaskCandidateAreaVO> data = new ArrayList<>();
            for (String taskId : taskIds){
                 ResultListEntity<TaskCandidateAreaVO> candidateProvinceByTaskId = areaClient.getCandidateProvinceByTaskId(taskId);
                 if (candidateProvinceByTaskId == null || CollectionUtils.isEmpty(candidateProvinceByTaskId.getData())){
                     continue;
                 }
                 data.addAll(candidateProvinceByTaskId.getData()) ;
             }
            if (CollectionUtils.isEmpty(data)){
                return Collections.emptyList();
            }
            List<String> provinceCodes = enterpriseExamineeInfoMapper.getExamineeProvinceCodeByExamId(examIds.toArray(new String[examIds.size()]));
            Iterator<TaskCandidateAreaVO> iterator = data.iterator();
            while (iterator.hasNext()){
                TaskCandidateAreaVO vo = iterator.next();
                if(!provinceCodes.contains(vo.getAreaId())){
                    iterator.remove();
                }
            }

            List<String> areaIds = new ArrayList<>();//用来临时存储对象

            List<TaskCandidateAreaVO> areaVOs = data.stream().filter(// 过滤去重
                    v -> {
                        boolean flag = !areaIds.contains(v.getName());
                        areaIds.add(v.getName());
                        return flag;
                    }
            ).collect(Collectors.toList());
            return areaVOs;

        }else {

            ResultListEntity<TaskCandidateAreaVO> candidateProvinceByTaskId = areaClient.getCandidateProvinceByTaskId(examInfo.getTaskId());
            List<TaskCandidateAreaVO> data = candidateProvinceByTaskId.getData();
            if (CollectionUtils.isEmpty(data)){
                return Collections.emptyList();
            }
            List<String> provinceCodes = enterpriseExamineeInfoMapper.getExamineeProvinceCodeByExamId(new String[]{examInfo.getId()});
            Iterator<TaskCandidateAreaVO> iterator = data.iterator();
            while (iterator.hasNext()){
                TaskCandidateAreaVO vo = iterator.next();
                if(!provinceCodes.contains(vo.getAreaId())){
                    iterator.remove();
                }
            }
            return data;
        }

    }

    @Override
    public List<TaskCandidateAreaVO> getCandidateCityByExamId(String examId, String province) {
        ExamInfo examInfo = getExamInfo(examId);
        ResultListEntity<TaskCandidateAreaVO> candidateProvinceByTaskId = areaClient.getCandidateCityById(examInfo.getTaskId(), province);
        List<TaskCandidateAreaVO> data = candidateProvinceByTaskId.getData();
        if (data == null){
            return new ArrayList<>();
        }
        return data;
    }


    @Override
    public List<TaskCandidateAreaVO> getCandidateProvinceByTaskId(String taskId) {
        ResultListEntity<TaskCandidateAreaVO> candidateProvinceByTaskId = areaClient.getCandidateProvinceByTaskId(taskId);
        List<TaskCandidateAreaVO> data = candidateProvinceByTaskId.getData();
        return data;
    }

    @Override
    public List<TaskCandidateAreaVO> getCandidateCityById(String taskId, String province) {
        ResultListEntity<TaskCandidateAreaVO> candidateProvinceByTaskId = areaClient.getCandidateCityById(taskId, province);
        List<TaskCandidateAreaVO> data = candidateProvinceByTaskId.getData();
        return data;
    }

    /**
     * 检查考试是否存在
     *
     * @param examId
     * @return
     */
    private ExamInfo getExamInfo(String examId) {
        ExamInfo examInfo = examInfoMapper.selectExamInfoById(examId);
        if (examInfo == null || StringUtils.isEmpty(examInfo.getId())) {
            throw new CustomException("要查询的考试不存在");
        }
        return examInfo;
    }

}
